
<template>
  <el-dialog :close-on-click-modal="false" :title="$tt('label_tms_waybill_task_intercept')" :visible.sync="visible" custom-class="dialog_auto"
    v-loading="loading">

    <el-alert v-if="isINTERCEPTED" type="warning" title="存在已拦截的任务,请重新选择！" :closable="false" style="margin-bottom: 15px;">

    </el-alert>
    <el-form :disabled="isINTERCEPTED" :label-width="$globalCss.formLabelWidth_default" :model="form" :rules="rules" ref="Form">

      <el-form-item :label="$tt('label_tms_interception_cause')" prop="interceptReason">
        <ex_super_input v-model="form.interceptReason" class="form_input_small" type="select" :attrs="{
          enum: 'InterceptReasonEnum'
        }" />
      </el-form-item>
      <el-form-item :label="$tt('label_tms_initiator')" prop="initiator">
        <ex_super_input v-model="form.initiator" class="form_input_small" type="select" :attrs="{
          enum: 'InitiatorEnum'
        }" />
      </el-form-item>
      <el-form-item :label="$tt('label_tms_cargo_handling_mode')" prop="processingMethod">
        <ex_super_input v-model="form.processingMethod" class="form_input_small" type="select" :attrs="{
          enum: 'ProcessingMethodEnum'
        }" />
      </el-form-item>
      <el-form-item :label="$tt('label_tms_incur_trans_costs')" prop="isTransportFee">
        <ex_super_input v-model="form.isTransportFee" class="form_input_small" type="select" :attrs="{
          enum: 'YesOrNoEnum'
        }" />
      </el-form-item>
      <el-form-item :label="$tt('label_tms_freight')" :prop="form.isTransportFee==='Y'?'transportFee':''">
        <ex_super_input v-model="form.transportFee" class="form_input_small" />
      </el-form-item>
      <el-form-item :label="$tt('label_tms_address')"
        prop="address"
        :rules="[
          { required:['RETURN_SET_OUT_PLACE','MODIFY_PURPOSE_PLACE'].includes(form.processingMethod)? true:false, message: $tt('tip_can_no_empty'), trigger: 'blur' },
        ]"
        >
        <ex_super_input v-model="form.address" class="form_input_small"/>
      </el-form-item>
      <el-form-item :label="$tt('label_tms_track_consignee')" prop="deliveryName"
      :rules="[
          { required:['RETURN_SET_OUT_PLACE','MODIFY_PURPOSE_PLACE'].includes(form.processingMethod)? true:false, message: $tt('tip_can_no_empty'), trigger: 'blur' },
        ]"
      >
        <ex_super_input v-model="form.deliveryName" class="form_input_small"/>
      </el-form-item>
      <el-form-item :label="$tt('label_tms_track_contact')" prop="deliveryContact"
      :rules="[
          { required:['RETURN_SET_OUT_PLACE','MODIFY_PURPOSE_PLACE'].includes(form.processingMethod)? true:false, message: $tt('tip_can_no_empty'), trigger: 'blur' },
        ]"
      >
        <ex_super_input v-model="form.deliveryContact" class="form_input_small"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog_footer">
      <el-button @click="visible = false">{{ $tt('label_cancel') }}</el-button>
      <el-button  :disabled="isINTERCEPTED" type="primary" @click="submitReady">{{ $tt('label_submit') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>


const default_obj = {
  interceptReason: "GOODS_TRANSPORT_WRONG",//	InterceptReasonEnum 	拦截原因: GOODS_TRANSPORT_WRONG 货物不正常运输, GOODS_WRONG 货物异常, RETURN_WITH 需返回处理, NOT_DELIVERY 收货人不能收货, OTHER 其他
  initiator: "MYSELF",//	InitiatorEnum 	发起方: CUSTOMER 客户, CARRIER 承运商, MYSELF 我司
  processingMethod: "RETURN_WAIT",//ProcessingMethodEnum 	货物处理方式: RETURN_SET_OUT_PLACE 返回出发地, MODIFY_PURPOSE_PLACE 更改目的地, RETURN_WAIT 返回待处理
  isTransportFee: "Y",//	YesOrNoEnum 	是否产生运输费: Y 是, N 否
  transportFee: "",//		bigdecimal 	运费
  provinceName: "",//		string 	终点省份
  cityName: "",//	string 	终点市
  countyName: "",//		string 	终点区
  address: "",//string 	地址
  deliveryName: "",//string 	收货人
  deliveryContact: "",//		string 	联系方式
}
export default {
  components: {},
  data() {
    return {

      visible: false,
      loading: false,
      isINTERCEPTED:false,
      form: _.cloneDeep(default_obj),

      itemList: [],
      rules: {
        interceptReason: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        initiator: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        processingMethod: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        isTransportFee: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        transportFee: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        address: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        deliveryName: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        deliveryContact: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
      },
    }
  },
  methods: {


    show(item,) {
      this.visible = true
      this.isINTERCEPTED = item.some(item=>item.mainStatus==='INTERCEPTED')
      this.itemList = Object.assign(_.cloneDeep(item))
      this.form = _.cloneDeep(default_obj)
    },

    submitReady() {
      this.$refs['Form'].validate((valid,obj) => {
        console.log(obj)
        if (valid) {
          this.submit()
        } else {
          this.$message.error(this.$tt('form_validate_error'))
          return false;
        }
      });
    },
    hide() {
      this.visible = false
      this.$refs['Form'].resetFields();
    },
    submit() {


      this.$ajax({
        url: "/tms-api/waybill/TmsTaskInfoOperationRpc/taskIntercept.json",
        method: 'post', type: 'json',
        loading: true,
        data: {
          ...this.form,
          waybillIdAndVersionList: this.itemList.map(it => {
            return {
              id: it.id,
              version: it.version,
            }
          }),
        },
        success: (res) => {
          this.$alert('勾选的任务已拦截，系统会发信息提醒服务商及司机，请关注任务拦截后的执行情况', '任务拦截提醒', {
            confirmButtonText: '确定',
            callback: action => {

            }
          });
          this.hide()
          this.$emit('submitSuccess')

        },
      })


    },

  }
}
</script>
